#ifndef __UI_Gauge_SPI_C
#define __UI_Gauge_SPI_C

#include "UI_Gauge_SPI.h"

void SPI_Gauge_init(){
	  GPIO_Config_T  configStruct;
	  SPI_Config_T   spiConfig;
	
    RCM_EnableAPB2PeriphClock(Gauge_RCM);
   
	  // SPI引脚初始化：
	  // SCLK MOSI
	  configStruct.pin = SPI_SCLK_PinId | SPI_MOSI_PinId;           
	  configStruct.mode = GPIO_MODE_AF_PP;
    #if defined AT32F403AVGT7 && UIGauge_spi_pullup > 0
	  configStruct.gpio_pull = GPIO_PULL_UP; 
	  #endif 			
	  GPIO_Config(Gauge_PinPort, &configStruct);       
	  // MISO
	  configStruct.pin = SPI_MISO_PinId;           
	  configStruct.mode = GPIO_MODE_IN_FLOATING; 
    #if defined AT32F403AVGT7 && UIGauge_spi_pullup > 0
	  configStruct.gpio_pull = GPIO_PULL_UP; 
	  #endif 		
	  GPIO_Config(Gauge_PinPort, &configStruct);
		// CS
		configStruct.pin = SPI_CS_PinId;           
	  configStruct.mode = GPIO_MODE_OUT_PP;  
    #if defined AT32F403AVGT7 && UIGauge_spi_pullup > 0
	  configStruct.gpio_pull = GPIO_PULL_UP; 
	  #endif 			
	  GPIO_Config(Gauge_PinPort, &configStruct);		
	  
	  // SPI功能初始化：    
    RCM_EnableAPB2PeriphClock(Gauge_SPI_RCM); // RCM_APB2_PERIPH_SPI1 		
	  SPI_ConfigStructInit(&spiConfig);
		
    // 字节长度
    spiConfig.length = SPI_DATA_LENGTH_8B;
		
    // 波特率分频：SPI_BAUDRATE_DIV_16 数字越小，SPI速度越快。
		#ifdef AT32F403AVGT7
			#if UI_Meter_Method == Meter_GuageIC_RN8209 
				spiConfig.baudrateDiv = SPI_BAUDRATE_DIV_32; //SPI_BAUDRATE_DIV_32
			#elif UI_Meter_Method == Meter_GuageIC_RN8302B
				spiConfig.baudrateDiv = SPI_BAUDRATE_DIV_32; // 速度太块，读取芯片版本号会有误。尽可能放慢些
			#else
				spiConfig.baudrateDiv = SPI_BAUDRATE_DIV_64;
			#endif
		#else
			#if UI_Meter_Method == Meter_GuageIC_RN8209 
				spiConfig.baudrateDiv = SPI_BAUDRATE_DIV_16; //SPI_BAUDRATE_DIV_32
			#elif UI_Meter_Method == Meter_GuageIC_RN8302B
				spiConfig.baudrateDiv = SPI_BAUDRATE_DIV_32; // 速度块，读取芯片版本号有误；但是读取数据却是正确的。
			#else
				spiConfig.baudrateDiv = SPI_BAUDRATE_DIV_32;
			#endif
		#endif
		
    // 两线全双工
    spiConfig.direction = SPI_DIRECTION_2LINES_FULLDUPLEX;
    // 高位先行:
    spiConfig.firstBit = SPI_FIRSTBIT_MSB; 
    // 主设备模式：
    spiConfig.mode = SPI_MODE_MASTER;
    // SPI时钟极性CPOL = 0：闲时低电平。
    spiConfig.polarity = SPI_CLKPOL_LOW;  
    // SPI片选模式选择：软件片选 or 硬件片选
    spiConfig.nss = SPI_NSS_SOFT;
    // SPI时钟相位CPHA = 1 = 第1个沿是写：采集数据是第2边沿。
    spiConfig.phase = SPI_CLKPHA_2EDGE;
    // SPI config： 
    SPI_Config(Gauge_SPI_id, &spiConfig);
		
    // 失能校验和		
    SPI_DisableCRC(Gauge_SPI_id);
      		
    // 使能SPI；
    SPI_Enable(Gauge_SPI_id);
		
		// CS初始化为高电平
		SPI_bit_set(SPI_CS_PinId);
	
		
}

// 过零引脚初始化
void rn_zero_init(){
	  GPIO_Config_T  configStruct;
	
	  #ifdef Zero_RCM
	    RCM_EnableAPB2PeriphClock(Zero_RCM);
		#else			
	    RCM_EnableAPB2PeriphClock(Gauge_RCM);
		#endif
	
	  // 过零引脚 初始化：
	  configStruct.pin = Zero_PinId;  
    configStruct.mode = GPIO_MODE_IN_FLOATING;  
    configStruct.speed = GPIO_SPEED_50MHz;   
   
    #ifdef Zero_PinPort			
	    GPIO_Config(Zero_PinPort, &configStruct);
		#else			
			GPIO_Config(Gauge_PinPort, &configStruct);
		#endif
	  
		#ifdef SYS_DEBUG
		printf("   rn_zero_init \r\n");
		#endif
		
}

// SPI发送
uint8_t SPI_SendRead_Byte(uint8_t data){
	  uint16_t timeout = 0xffff;
	  while (SPI_I2S_ReadStatusFlag(Gauge_SPI_id, SPI_FLAG_TXBE) == RESET && timeout--){
			 
		}
    SPI_I2S_TxData(Gauge_SPI_id, data);	
		timeout = 0xffff;
	  while (SPI_I2S_ReadStatusFlag(Gauge_SPI_id, SPI_FLAG_RXBNE) == RESET && timeout--){
			  
		}
    return (uint8_t)SPI_I2S_RxData(Gauge_SPI_id);
}


#endif

